package com.jbp.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.PoolXUserDao;
import com.jbp.model.Pool;
import com.jbp.model.PoolXUser;
import com.jbp.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import java.util.Map;

@Service
public class PoolXUserService extends ServiceImpl<PoolXUserDao, PoolXUser> implements IService<PoolXUser> {

    @Autowired
    private UserService userService;
    @Autowired
    private PoolService poolService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PoolXUser> queryWrapper = new QueryWrapper<>();
        if (params.get("userUuid") != null){
            queryWrapper.inSql("user_id", "select id from user WHERE uuid LIKE '%"+params.get("userUuid")+"%'");
        }
        if (params.get("userName") != null){
            queryWrapper.inSql("user_id", "select id from user WHERE name LIKE '%"+params.get("userName")+"%'");
        }
        if (params.get("id") != null) {
            queryWrapper.like("id",params.get("id"));
        }
        if (params.get("price") != null) {
            queryWrapper.like("price",params.get("price"));
        }
        if (params.get("poolId") != null) {
            queryWrapper.like("pool_id",params.get("poolId"));
        }
        if (params.get("userId") != null) {
            queryWrapper.like("user_id",params.get("userId"));
        }
        if (params.get("ip") != null) {
            queryWrapper.like("ip",params.get("ip"));
        }
        if (params.get("device") != null) {
            queryWrapper.like("device",params.get("device"));
        }
        if (params.get("expectPay") != null) {
            queryWrapper.like("expect_pay",params.get("expectPay"));
        }
        if (params.get("pay") != null) {
            queryWrapper.like("pay",params.get("pay"));
        }
        if (params.get("income") != null) {
            queryWrapper.like("income",params.get("income"));
        }
        if (params.get("del") != null) {
            queryWrapper.like("del",params.get("del"));
        }
        if (params.get("createTime") != null) {
            queryWrapper.like("create_time",params.get("createTime"));
        }
        if (params.get("updateTime") != null) {
            queryWrapper.like("update_time",params.get("updateTime"));
        }
        if (params.get("fromDate")!=null) {
            queryWrapper.ge("create_time", params.get("fromDate"));
        }
        if (params.get("toDate")!=null) {
            queryWrapper.le("DATE_FORMAT(create_time,   '%Y-%m-%d')", params.get("toDate"));
        }
        queryWrapper.orderByDesc("id");
        IPage<PoolXUser> page = this.page(
            new Query<PoolXUser>().getPage(params), queryWrapper
        );
        page.getRecords().forEach(record -> {
            User user = userService.getById(record.getUserId());
            record.setUserUuid(user.getUuid());
            record.setUserName(user.getName());
        });
        return new PageUtils(page);
    }

    public Pool getPool(Integer userId, Integer price) {
        QueryWrapper<PoolXUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("del", 0);
        queryWrapper.eq("price", price);
        PoolXUser one = getOne(queryWrapper);

        if (one.getExpectPay()<= one.getPay()) {
            one.setDel(1);
            updateById(one);
            return getPool(userId, price);
        }

        return poolService.getById(one.getPoolId());
    }

    public PoolXUser getByPrice(Integer userId, Integer price) {
        User user = userService.getById(userId);
        PoolXUser poolXUser = getBaseMapper().getByUser(userId, user.getDevice(), user.getIp(), price);
        return poolXUser;
    }
}